//
//  ZhuanShuController.m
//  SleepingElf
//
//  Created by karong on 2023/11/9.
//

#import "ZhuanShuController.h"
#import "ZhuanShuRequest.h"
#import "ZhuanShuCell.h"
#import "ZhuanShuModel.h"

@interface ZhuanShuController ()
@property (weak, nonatomic) IBOutlet UICollectionView *listView;
@end

@implementation ZhuanShuController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColor.clearColor;
    ZhuanShuRequest * request = [ZhuanShuRequest new];
    self.request = request;
    [self addObserver];
}

- (void)addObserver {
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(beginRefresh) name:SELoginSuccessNotification object:nil];
}

- (BOOL)disableAutoBeginRefresh {
    return YES;
}

- (void)loadCustomMusicDetail:(NSInteger)custom_id {
    [ZhuanShuRequest loadZhuanShuDetailWithId:custom_id success:^(NSArray *array) {
        for (MusicModel * music in array) {
            music.is_collect = YES;
            break;
        }
        [[PlayerManager shared]playWithMusics:array];
    } failed:^(NSURLResponse * _Nonnull response, NSError * _Nonnull error, id  _Nonnull responseObject) {
        
    }];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ZhuanShuCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ZhuanShuCell" forIndexPath:indexPath];
    [cell setDataWithObject:self.dataArray[indexPath.row]];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGSize size = CGSizeZero;
    size.width = SEScreenWidth - 13 - 13;
    size.height = 74;
    return size;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    ZhuanShuModel * zhuanShu = self.dataArray[indexPath.row];
    [self loadCustomMusicDetail:zhuanShu.id];
}

@end
